package com.ruoyi.mixuebingcheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.mixuebingcheng.domain.bo.ViewOrderBo;
import com.ruoyi.mixuebingcheng.domain.vo.ViewOrderVo;
import com.ruoyi.mixuebingcheng.domain.ViewOrder;
import com.ruoyi.mixuebingcheng.mapper.ViewOrderMapper;
import com.ruoyi.mixuebingcheng.service.IViewOrderService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * VIEWService业务层处理
 *
 * @author ruoyi
 * @date 2023-10-27
 */
@RequiredArgsConstructor
@Service
public class ViewOrderServiceImpl implements IViewOrderService {

    private final ViewOrderMapper baseMapper;

    /**
     * 查询VIEW
     */
    @Override
    public ViewOrderVo queryById(Long userId){
        return baseMapper.selectVoById(userId);
    }

    /**
     * 查询VIEW列表
     */
    @Override
    public TableDataInfo<ViewOrderVo> queryPageList(ViewOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ViewOrder> lqw = buildQueryWrapper(bo);
        Page<ViewOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询VIEW列表
     */
    @Override
    public List<ViewOrderVo> queryList(ViewOrderBo bo) {
        LambdaQueryWrapper<ViewOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ViewOrder> buildQueryWrapper(ViewOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ViewOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, ViewOrder::getUserId, bo.getUserId());
        lqw.eq(bo.getOrderGoodId() != null, ViewOrder::getOrderGoodId, bo.getOrderGoodId());
        lqw.eq(bo.getOrderId() != null, ViewOrder::getOrderId, bo.getOrderId());
        lqw.eq(bo.getGoodId() != null, ViewOrder::getGoodId, bo.getGoodId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ViewOrder::getName, bo.getName());
        lqw.eq(bo.getPrice() != null, ViewOrder::getPrice, bo.getPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), ViewOrder::getImage, bo.getImage());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), ViewOrder::getCode, bo.getCode());
        lqw.eq(bo.getStore() != null, ViewOrder::getStore, bo.getStore());
        lqw.eq(bo.getIsNew() != null, ViewOrder::getIsNew, bo.getIsNew());
        lqw.eq(bo.getAddressBookId() != null, ViewOrder::getAddressBookId, bo.getAddressBookId());
        lqw.eq(StringUtils.isNotBlank(bo.getConsignee()), ViewOrder::getConsignee, bo.getConsignee());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), ViewOrder::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getAreaCode()), ViewOrder::getAreaCode, bo.getAreaCode());
        lqw.like(StringUtils.isNotBlank(bo.getProvinceName()), ViewOrder::getProvinceName, bo.getProvinceName());
        lqw.like(StringUtils.isNotBlank(bo.getCityName()), ViewOrder::getCityName, bo.getCityName());
        lqw.like(StringUtils.isNotBlank(bo.getDistrictName()), ViewOrder::getDistrictName, bo.getDistrictName());
        lqw.eq(bo.getNumber() != null, ViewOrder::getNumber, bo.getNumber());
        lqw.eq(bo.getAmount() != null, ViewOrder::getAmount, bo.getAmount());
        lqw.eq(bo.getStatus() != null, ViewOrder::getStatus, bo.getStatus());
        lqw.eq(bo.getCouponId() != null, ViewOrder::getCouponId, bo.getCouponId());
        return lqw;
    }

    /**
     * 新增VIEW
     */
    @Override
    public Boolean insertByBo(ViewOrderBo bo) {
        ViewOrder add = BeanUtil.toBean(bo, ViewOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setUserId(add.getUserId());
        }
        return flag;
    }

    /**
     * 修改VIEW
     */
    @Override
    public Boolean updateByBo(ViewOrderBo bo) {
        ViewOrder update = BeanUtil.toBean(bo, ViewOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ViewOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除VIEW
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
